#! /usr/bin/env python
# ididdoview.py
'''
This Module container the view tier for the todo application

ConkyView       - Class that generates the output for conky
CommandLineView - Class that generates the output in the command line
'''
__author__ = "Tiago Seixas"
__copyright__ = "Copyright 2011, Tiago Seixas"
__credits__ = ["Tiago Seixas"]
__license__ = "GPL"
__version__ = "3"
__maintainer__ = "Tiago Seixas"
__email__ = "tiagoafseixas@gmail.com"
__status__ = "Prototype"

class ConkyView:
    '''
    Class handler for the conky output
    '''

    default_format = '[{0.ref_id:0>2}] - {0.title}\n'\
        '         {0.where} {0.project} {0.when} {0.priority}'

    field_separator = ' '

    def __init__(self, items,format = None, separator = None):
        '''
        Init function for the ConkyView class

        Parameters:
          items - a list of todo items
        '''
        self.items = items

        if format is not None:
            format = format.replace('$','0.')
            self.default_format = format.replace('\\n', '\n')

        if separator is not None:
            self.field_separator = separator

    def render(self):
        '''
        Render the output for conky
        '''

        for item in self.items:
            if item.where is None:
                item.where = self.field_separator + ' @ Anywhere'
            else:
                item.where = self.field_separator + ' @ ' + item.where

            if item.project is not None:
                item.project = self.field_separator + ' Project: ' + item.project

            if item.when is not None:
                item.when = self.field_separator + ' Due: ' + item.when
            else:
                item.when = ''

            if item.priority > 0:
                item.priority = self.field_separator + ' #' + str(item.get_effective_priority())
            if item.created is not None:
                item.created = self.field_separator + ' Created: ' + item.created
            if item.done is not None:
                item.done = self.field_separator + ' Completed: ' + str(item.done)

            print(self.default_format.format(item))



class CommandLineView:
    '''
    Class handler for command line output
    '''

    command_line_format = '[{0.ref_id:0>2}] {0.title}\n'\
        '     Priority:{0.priority}\n'\
        '     Where   :{0.where}\n'\
        '     Created :{0.created}\n'\
        '     Due     :{0.when}\n'\
        '     Project :{0.project}\n'\
        '     Done    :{0.done}\n'

    hruler = '----------------------------------------------------------'
    def __init__(self, items):
        '''
        Init function for the CommandLineView Class

        Parameters:
          items - a list of todo items
        '''
        self.items = items

    def render(self):
        '''
        Renders the command line output for all items in the list
        '''

        print(self.hruler)
    
        for item in self.items:
            item.priority = str(item.get_effective_priority())
            print self.command_line_format.format(item)

        print(self.hruler)

    def render_complete(self):
        #!!! TODO: This function
        pass
